﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;


namespace SpriterContentPipelineExtension
{




    /// <remarks/>
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterData, SpriterRuntime")]
    
    [System.Xml.Serialization.XmlRoot("spriter_data")]
    public partial class SpriterData
    {
        [XmlIgnore]
        [ContentSerializerIgnore]
        public string pathtofile;
        // Single texture atlas combining all separate sprite images
        [XmlIgnore]
        public Texture2DContent Texture = new Texture2DContent();
        // List containing location of each sprint on single texture
        [XmlIgnore]
        public List<List<Rectangle>> ImageRectangles = new List<List<Rectangle>>();

        /// <remarks/>
        [XmlElement("folder")]
        public List<SpriterFolder> folders;

        /// <remarks/>
        [XmlElement("entity")]
        public List<SpriterEntity> entities;

        /// <remarks/>
        [XmlAttribute("scml_version")]
        public string scml_version;

        /// <remarks/>
        [XmlAttribute("generator")]
        public string generator;

        /// <remarks/>
        [XmlAttribute("generator_version")]
        public string generator_version;

        [XmlAttribute("pixel_art_mode")]
        public bool pixel_art_mode;
    }

    /// <remarks/>
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterFolder, SpriterRuntime")]
    
    public partial class SpriterFolder
    {
        /// <remarks/>
        [XmlElement("file")]
        public List<SpriterFolderFile> file;

        /// <remarks/>
        [XmlAttribute("id")]
        public int FolderId;

        /// <remarks/>
        [XmlAttribute("name")]
        public string FolderName;
    }

    /// <remarks/>
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterFolderFile, SpriterRuntime")]
    
    public partial class SpriterFolderFile
    {
        /// <remarks/>
        [XmlAttribute("id")]
        public int FileId;

        /// <remarks/>
        [XmlAttribute("name")]
        public string FileName;

        /// <remarks/>
        [XmlAttribute("width")]
        public int width;

        /// <remarks/>
        [XmlAttribute("height")]
        public int height;
    }

    /// <remarks/>
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterEntity, SpriterRuntime")]
    
    public partial class SpriterEntity
    {
        /// <remarks/>
        [XmlElement("animation")]
        public List<SpriterEntityAnimation> animations;

        /// <remarks/>
        [XmlAttribute("id")]
        public int EntityId;

        /// <remarks/>
        [XmlAttribute("name")]
        public string EntityName;
    }

    /// <remarks/>

    
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterEntityAnimation, SpriterRuntime")]

    public partial class SpriterEntityAnimation
    {

        /// <remarks/>
        [XmlArrayItem("key", IsNullable = false)]
        public List<SpriterKeyFrame> mainline;

        /// <remarks/>
        [XmlElement("timeline")]
        public List<SpriterTimeline> Timelines;

        /// <remarks/>
        [XmlAttribute("id")]
        public int AnimationId;

        /// <remarks/>
        [XmlAttribute("name")]
        public string AnimationName;

        /// <remarks/>
        [XmlAttribute("length")]
        public int length;

        /// <remarks/>
        [XmlAttribute("looping")]
        public bool looping;
    }

    /// <remarks/>

    
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterKeyFrame, SpriterRuntime")]
    public partial class SpriterKeyFrame
    {

        /// <remarks/>
        [XmlAttribute("id")]
        public int KeyId;

        /// <remarks/>
        [XmlAttribute("time")]
        public ushort time;

        /// <remarks/>
        [XmlElement("bone_ref")]
        public List<SpriterBoneTimelineKeyMapping> bone_refs;

        /// <remarks/>
        [XmlElement("object_ref")]
        public List<SpriterObjectTimelineKeyMapping> object_refs;


    }

    
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterBoneTimelineKeyMapping, SpriterRuntime")]
    public partial class SpriterBoneTimelineKeyMapping
    {
        /// <remarks/>
        [ContentSerializer(ElementName = "BoneId")]
        [XmlAttribute("id")]
        public int BoneId;
        
        /// <remarks/>
        [ContentSerializer(ElementName = "KeyId")]
        [XmlAttribute("key")]
        public int KeyId;

        [XmlAttribute("parent")]
        [ContentSerializer(ElementName = "ParentId")]
        public int ParentId { get; set; }

        [ContentSerializer(ElementName = "ParentIdSpecified")]
        [XmlIgnore]
        public bool ParentIdSpecified;

        [ContentSerializer(ElementName = "TimelineId")]
        [XmlAttribute("timeline")]
        public int TimelineId;

       
    }

    /// <remarks/>
    
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterObjectTimelineKeyMapping, SpriterRuntime")]
    public partial class SpriterObjectTimelineKeyMapping
    {

        /// <remarks/>
        [XmlAttribute("id")]
        public int ObjectId;

        [XmlAttribute("parent")]
        public int ParentId { get; set; }

        [XmlIgnore]
        public bool ParentIdSpecified;

        /// <remarks/>
        [XmlAttribute("timeline")]
        public int TimelineId;

        /// <remarks/>
        [XmlAttribute("key")]
        public int KeyId;

        /// <remarks/>
        [XmlAttribute("z_index")]
        public int z_index;
    }

    /// <remarks/>

    

    [ContentSerializerRuntimeType("SpriterRuntime.SpriterTimeline, SpriterRuntime")]

    public partial class SpriterTimeline
    {

        /// <remarks/>
        [XmlElement("key")]
        public List<SpriterTimelineKey> keys;

        /// <remarks/>
        [XmlAttribute("id")]
        public int TimelineId;
    }

    /// <remarks/>

    

    [ContentSerializerRuntimeType("SpriterRuntime.SpriterTimelineKey, SpriterRuntime")]

    public partial class SpriterTimelineKey
    {

        /// <remarks/>
        [XmlElement("object")]
        public SpriterTimelineKeyObject KeyObject;

        /// <remarks/>
        [XmlElement("bone")]
        public SpriterTimelineKeyBone KeyBone;

        /// <remarks/>
        [XmlAttribute("id")]
        public int KeyId;

        /// <remarks/>
        [XmlAttribute("spin")]
        public sbyte spin;

        /// <remarks/>
        [XmlAttribute("time")]
        public int time;
    }

    /// <remarks/>

    
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterTimelineKeyObject, SpriterRuntime")]

    //SpriterEntityAnimationTimelineKeyObject
    public partial class SpriterTimelineKeyObject : IXmlSerializable
    {
        //object_type	string
        //atlas	        int
        //folder	    int
        //file	        int
        //usage	        string
        //blend_mode	string
        //name	        string
        //x	            float
        //y	            float
        //pivot_x	    float
        //pivot_y	    float
        //angle	        float
        //w	            float
        //h	            float
        //scale_x	    float
        //scale_y	    float
        //r	            float
        //g	            float
        //b	            float
        //a	            float
        //variable_type	string
        //value	        variable_type
        //min	        variable_type
        //max	        variable_type
        //animation	    int
        //t	            float
        //z_index	    int
        //volume	    float
        //panning	    float

        /// <remarks/>
        //[XmlAttribute("folder")]
        public int FolderId;

        /// <remarks/>
        //[XmlAttribute("file")]
        public int FileId;


        public Vector2 Position;
        public Vector2 Pivot;
        public Vector2 scale;

        /// <remarks/>
        //[XmlAttribute("angle")]
        public float angle;

        /// <remarks/>
        //[XmlAttribute("a")]
        public float opacity;

        public System.Xml.Schema.XmlSchema GetSchema() { return null; }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("folder", FolderId.ToString());
            writer.WriteAttributeString("file", FileId.ToString());
            writer.WriteAttributeString("x", Position.X.ToString());
            writer.WriteAttributeString("y", Position.Y.ToString());
            writer.WriteAttributeString("a", opacity.ToString());
            writer.WriteAttributeString("angle", angle.ToString());
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.MoveToContent();
            FolderId = int.Parse(reader.GetAttribute("folder"));
            FileId = int.Parse(reader.GetAttribute("file"));
            float x = float.Parse(reader.GetAttribute("x") ?? 0.ToString());
            float y = float.Parse(reader.GetAttribute("y") ?? 0.ToString());

            Position = new Vector2(x, y);
            float width = float.Parse(reader.GetAttribute("scale_x") ?? 1.ToString());
            float height = float.Parse(reader.GetAttribute("scale_y") ?? 1.ToString());
            scale = new Vector2(width, height);

            x = float.Parse(reader.GetAttribute("pivot_x") ?? 0.ToString());
            y = float.Parse(reader.GetAttribute("pivot_y") ?? 1.ToString());
            Pivot = new Vector2(x, y);
           
            opacity = float.Parse(reader.GetAttribute("a") ?? 1.ToString());
            angle = float.Parse(reader.GetAttribute("angle") ?? 0.ToString());
        }
    }

    
    [ContentSerializerRuntimeType("SpriterRuntime.SpriterTimelineKeyBone, SpriterRuntime")]

    //SpriterEntityAnimationTimelineKeyObject
    public partial class SpriterTimelineKeyBone : IXmlSerializable
    {


        [ContentSerializer]
        public Vector2 Position;
        [ContentSerializer]
        public Vector2 scale;

        [ContentSerializer]
        public float angle;

        [ContentSerializer]
        public float opacity;

        public System.Xml.Schema.XmlSchema GetSchema() { return null; }

        public void WriteXml(System.Xml.XmlWriter writer)
        {

            writer.WriteAttributeString("x", Position.X.ToString());
            writer.WriteAttributeString("y", Position.Y.ToString());
            writer.WriteAttributeString("a", opacity.ToString());
            writer.WriteAttributeString("angle", angle.ToString());
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.MoveToContent();
            float x = float.Parse(reader.GetAttribute("x") ?? 0.ToString());
            float y = float.Parse(reader.GetAttribute("y") ?? 0.ToString());

            Position = new Vector2(x, y);
            float width = float.Parse(reader.GetAttribute("scale_x") ?? 1.ToString());
            float height = float.Parse(reader.GetAttribute("scale_y") ?? 1.ToString());
            scale = new Vector2(width, height);
            opacity = float.Parse(reader.GetAttribute("a") ?? 0.ToString());
            angle = float.Parse(reader.GetAttribute("angle") ?? 0.ToString());
        }
    }
}
